package com.ejoysoft.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ejoysoft.common.Globa;
import com.ejoysoft.wlanyz.business.ResultXML;


public class ResultServlet extends HttpServlet implements Servlet
{
	private static final long serialVersionUID = 217251451801586160L;
	public ResultServlet()
	{
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException {	// TODO Auto-generated method stub
		try {
			this.execute(req, resp);
		} catch(Exception e) {
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}

	private void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException
	{
		 Globa globa = new Globa();
		 ResultXML result = new ResultXML(globa,true);
		 req.setCharacterEncoding("utf-8");
		 resp.setCharacterEncoding("utf-8");
		 String content =  req.getParameter("testResult");
		 if(content !="" && content != null ){
			 InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"));//注意content的值是否为空，做判断。
			 boolean i =  result.readresult(is);
			  if(i == true){
				  resp.getWriter().write(returnResult("OK").toString());
			  }else {
				  resp.getWriter().write(returnResult("NO").toString());
			}
			 
		 }else{
			 resp.getWriter().write(returnResult("NO").toString());
		 }

	     globa.closeCon();
 
	}
	

	private StringBuffer returnResult(String result)
	{
		StringBuffer sbReturnContent = new StringBuffer("<?xml version='1.0' encoding='utf-8'?> ");
		sbReturnContent.append("<info>");
		sbReturnContent.append("<result>"+result+"</result>");
		sbReturnContent.append("</info>");
		return sbReturnContent;
	}
	

}

